/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"GVM/Utils"
	"fmt"
	"github.com/AlecAivazis/survey/v2"
	"github.com/spf13/cobra"
	"regexp"
)

// listCmd represents the list command
var listCmd = &cobra.Command{
	Use:   "list",
	Short: "获取本地或者远端版本列表",
	Long: `
	list 返回本地版本列表
	-r 返回远端版本列表
	`,
	Run: func(cmd *cobra.Command, args []string) {
		remote, _ := cmd.Flags().GetBool("remote")

		if remote {
			// 远端版本列表
			var guiguiutil = Utils.NEWGUIGUIVersionUtil()
			versions, err := guiguiutil.GetGUIGUIVersionList()
			if err != nil {
				fmt.Println("请求或解析失败:", err)
				return
			}
			fmt.Printf("获取到的版本数量: %d\n", versions.Data.Count)
			var versionOptions []string
			for _, version := range versions.Data.VersionList {
				versionOptions = append(versionOptions,
					fmt.Sprintf("%d\t %s\t %s\t %s\t ", version.ID, version.Title, version.DownloadURL, version.CreateTime))
			}
			var selected string
			prompt := &survey.Select{
				Message: "请选择要安装的版本:",
				Options: versionOptions,
			}
			survey.AskOne(prompt, &selected)
			// 根据选中的id 获取 url 下载链接

			re := regexp.MustCompile(`^\\d+`)
			selectedID := re.FindString(selected)
			fmt.Println("你选择的ID是:", selectedID)
			// 下载链接

		} else {
			// 本地版本列表
			fmt.Println("获取本地版本列表")

		}

	},
}

func init() {
	rootCmd.AddCommand(listCmd)

	// Here you will define your flags and configuration settings.

	// Cobra supports Persistent Flags which will work for this command
	// and all subcommands, e.g.:
	// listCmd.PersistentFlags().String("foo", "", "A help for foo")

	// Cobra supports local flags which will only run when this command
	// is called directly, e.g.:
	// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

	// 添加 -r 选项
	listCmd.Flags().BoolP("remote", "r", false, "获取远端版本列表")
}
